package biz.datalk.industrialland.admin.modules.system.controller;

import biz.datalk.industrialland.admin.modules.system.pojo.dto.SysDepartDTO;
import biz.datalk.industrialland.admin.modules.system.pojo.vo.SysDepartVO;
import biz.datalk.industrialland.admin.modules.system.service.ISysDepartService;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.log.aspect.annotation.SysLog;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.validated.BatchDelete;
import biz.datalk.industrialland.common.validated.Delete;
import biz.datalk.industrialland.common.validated.Insert;
import biz.datalk.industrialland.common.validated.Update;
import biz.datalk.industrialland.mbg.pojo.po.SysDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tarofang@163.com
 * @date 2019-08-18
 */
@RestController
@RequestMapping("/sys/depart")
public class SysDepartController {

    private final ISysDepartService departService;

    @Autowired
    public SysDepartController(ISysDepartService departService) {
        this.departService = departService;
    }


    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysDepart.class, filter = "createTime,updateTime")
    @SysLog(OptEnum.LAND_DEPART_10401)
    public UnifyResult list(@RequestBody SysDepartDTO sysDepartDTO) {
        return departService.list(sysDepartDTO);
    }

    @RSAES
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_DEPART_10402)
    public UnifyResult add(@RequestBody @Validated({Insert.class}) SysDepartDTO sysDepartDTO) {
        return departService.add(sysDepartDTO);
    }

    @RSAES
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_DEPART_10403)
    public UnifyResult update(@RequestBody @Validated({Update.class}) SysDepartDTO sysDepartDTO) {
        return departService.update(sysDepartDTO);
    }

    @RSAES
    @PostMapping(value = "/batchDelete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_DEPART_10404)
    public UnifyResult batchDelete(@RequestBody @Validated({BatchDelete.class}) SysDepartDTO sysDepartDTO) {
        return departService.batchDelete(sysDepartDTO);
    }

    @RSAES
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_DEPART_10405)
    public UnifyResult delete(@RequestBody @Validated({Delete.class}) SysDepartDTO sysDepartDTO) {
        departService.delete(sysDepartDTO);
        return UnifyResult.success();
    }


    @RSAES
    @PostMapping(value = "/tree", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysDepartVO.class, filter = "createTime,updateTime,createBy,createName,updateBy,updateName,sub,hasSub,personCount")
    @SysLog(OptEnum.LAND_DEPART_10406)
    public UnifyResult tree(@RequestBody SysDepartDTO sysDepartDTO) {
        List<SysDepartVO> tree = departService.tree(sysDepartDTO);
        return UnifyResult.success().setData(tree);
    }

    @RSAES
    @PostMapping(value = "/treeAll", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysDepartVO.class, filter = "createTime,updateTime,createBy,createName,updateBy,updateName")
    @SysLog(OptEnum.LAND_DEPART_10407)
    public UnifyResult treeAll(@RequestBody SysDepartDTO sysDepartDTO) {
        List<SysDepartVO> tree = departService.treeAll(sysDepartDTO);
        return UnifyResult.success().setData(tree);
    }
}
